// Package service /*mysql相关
package service

import (
	cf "godocker/common/functions"
)

type mysql struct {
	SName string

	basicDir []string
	name     string
	pwd      string
	version  string
}

func mysqlNew() *mysql {
	i := installDir + "/lib/mysql"
	return &mysql{
		"mysql",
		[]string{
			i + "/conf",
			i + "/data",
			i + "/file",
			i + "/log",
			i + "/mysql-files",
		},
		cf.GetEnvVal(installDir+"/.env", "mysqlname="),
		cf.GetEnvVal(installDir+"/.env", "mysqlpwd="),
		cf.GetEnvVal(installDir+"/.env", "mysqlv="),
	}
}

func (m mysql) install() {
	m.mkdir()
	m.dockerPull()
}

func (m mysql) mkdir() {
	for _, v := range m.basicDir {
		cf.MakeDir(v)
	}
}

func (m mysql) dockerPull() {
	cf.ExecShell("docker run --name " + m.name + " -dp 3306:3306 -e MYSQL_ROOT_PASSWORD=" + m.pwd + " mysql:" + m.version)

	cf.ExecShell("docker cp " + m.name + ":/etc/mysql/. " + m.basicDir[0])

	cf.ExecShell("docker stop " + m.name)

	cf.ExecShell("docker rm " + m.name)
}
